<?php

namespace App\Console\Commands\Tools\OpenShop\Contract;

use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Library\Rms\RmsPayApi;
use App\Models\OpenShop\InvestmentPromotion\OpenShopContractTemplateModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPermissionAnchorModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Service\OpenShop\Feishu\ShopManualService;
use App\Service\OpenShop\OrderService;
use Illuminate\Console\Command;

class RefreshContractPolicyTemplateId extends Command
{
    protected $signature   = 'tools:open_shop:refresh_contract_policy_template_id {orderNo} {pid} {updatePrice=false}';
    protected $description = '刷新合同政策模板id';

    public function handle()
    {
        $orderNo = $this->argument('orderNo');

        $pid = (int)$this->argument('pid');

        OpenShopUserContractModel::query()
                                 ->where('orderNo', $orderNo)
                                 ->update(['policyTemplateId' => $pid]);

        if ($this->argument('updatePrice')) {
            $priceInfo = OpenShopContractTemplateModel::query()
                                                      ->where('id', $pid)
                                                      ->first();

            $shopCount = OpenShopUserContractModel::query()
                                                  ->where('orderNo', $orderNo)
                                                  ->first();

            $shopCount = $shopCount->shopCount;
            OpenShopUserContractModel::query()
                                     ->where('orderNo', $orderNo)
                                     ->update([
                                         'initialFee'                       => $shopCount * $priceInfo->initialFee,
                                         'technologyTransferFee'            => $shopCount * $priceInfo->technologyTransferFee,
                                         'deposit'                          => $shopCount * $priceInfo->deposit,
                                         'managementCost'                   => $shopCount * $priceInfo->managementCost,
                                         'managementCostEveryYear'          => $shopCount * $priceInfo->managementCostEveryYear,
                                         'decorationDeposit'                => $shopCount * $priceInfo->decorationDeposit,
                                         'guidanceFee'                      => $shopCount * $priceInfo->guidanceFee,
                                         'depositAmountRatio'               => $priceInfo->depositAmountRatio,
                                         'monthRealAmountCommissionCapping' => $priceInfo->monthRealAmountCommissionCapping,
                                         'isCommissionCapping'              => $priceInfo->isCommissionCapping,
                                         'amountCommissionCapping'          => $priceInfo->amountCommissionCapping,
                                         'depositAmount'                    => $shopCount * $priceInfo->depositAmount,
                                     ]);

            // 同步分账配置到rms
            (new RmsPayApi(null))->addMyBankShareConfig($orderNo);
        }

        $this->info('调整合同政策模版完成');

    }
}
